﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// String类型转换接口
    /// </summary>
    public class StringTypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public StringTypeConverter()
            : base(PrimitiveDataTypes.StringType)
        {

        }



        /// <summary>
        /// 将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <returns>转换后的值</returns>
        protected override object PrimitiveConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType)
        {
            object result;
            switch (valueType)
            {
                case PrimitiveDataTypes.StringType:
                    result = value;
                    break;
                default:
                    if (value != null)
                    {
                        result = value.ToString();
                    }
                    else
                    {
                        result = null;
                    }
                    break;
            }

            return result;
        }



        /// <summary>
        /// 转换数据到
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <param name="result">转换后的值,存放在object中,如果转换失败为目标类型的默认值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType, out object result)
        {
            bool parseRet = true;
            switch (valueType)
            {
                case PrimitiveDataTypes.StringType:
                    result = value;
                    break;
                default:
                    if (value != null)
                    {
                        result = value.ToString();
                    }
                    else
                    {
                        result = null;
                    }
                    break;
            }

            return parseRet;
        }

    }
}
